﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;


namespace TaoPortal.Areas.Admin.Models
{
    public class SessionEnt
    {
        public string UserName { get; set; }
        public string Roles { get; set; }
    }

    public class BaseModel
    {
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public SessionEnt SessionEnt { get; set; }
        public HttpSessionStateBase Session { get; set; }
        /// <summary>
        /// 构造树形结构数据
        /// </summary>
        /// <param name="parent">父节点</param>
        /// <param name="data">数据</param>
        public void GetTreeData(TreeData parent, List<TreeData> data)
        {
            parent.children = data.Where(x => x.parentid == parent.id).ToList();
            foreach (var item in parent.children)
            {
                GetTreeData(item, data);
            }
        }
        /// <summary>
        /// 根据父级节点获取子节点数据
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="parentid">父节点</param>
        /// <returns></returns>
        public List<TreeData> GetClassByPID(TaoPortal.Common.SysEnum.SysClassType type, string parentid)
        {
            if (string.IsNullOrEmpty(parentid))
                parentid = null;
            var rp = new SystemRepository();
            var allData = rp.GetAppClass(Convert.ToInt32(type).ToString());
            var data = allData.Where(x => x.ParentID == parentid).Select(x => new TreeData { leaf = allData.Where(y => y.ParentID == x.ID).Count() == 0, parentid = x.ParentID, text = x.ClassName, id = x.ID }).ToList();
            return data;
        }
        /// <summary>
        /// 根据分类类型获取分类树形结构数据
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public List<TreeData> GetClassByType(TaoPortal.Common.SysEnum.SysClassType type)
        {
            var rp = new SystemRepository();
            var allData = rp.GetAppClass(Convert.ToInt32(type).ToString());
            var data = allData.Select(x => new TreeData { leaf = allData.Where(y => y.ParentID == x.ID).Count() == 0, expanded = true, parentid = x.ParentID, text = x.ClassName, id = x.ID }).ToList();
            var classList = data.Where(x => x.parentid == null).ToList();
            foreach (var item in classList)
            {
                GetTreeData(item, data);
            }
            return classList;
        }
    }
}